<?php
namespace app\model;

/**
 * Author: cbw
 * Desc: 微信视频号自定义交易组件开发接口
 * Time: 2023/3/11 10:06
 * Email: 17854288794@163.com
 */
require_once PATH_MODULE .'define.php';
class Minishopopencomponent{

    /**
     * Desc:请求类
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/5 13:04
     * @param $url
     * @param false $post
     * @param array $header
     * @return bool|string
     */
    public static function curl($url, $post = false,$header = [])
    {
        $headerArray = array_merge($header, [
            'Expect:',
        ]);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($post) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result ? json_decode($result, true) : [];
    }

    public static function media_curl($url,$data,$file='')
    {
        if (!empty($file)){
            $data['media'] = new CURLFile($file);
        }
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $output = curl_exec($curl);
        if (curl_errno($curl)>0){
            echo curl_error($curl);
            $output = 'http请求出错'.'['.curl_error($curl).']';
        }
        curl_close($curl);
        return $output;
    }


    /**********************申请接入接口*********************/
    /**
     * Desc:接入申请
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 09:13
     * @param $system
     * @return array
     */
    public static function registerApply($system)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/register/apply?access_token='.$access_token;
        $data = self::curl($url,json_encode([]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:获取接入状态
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 09:17
     * @param $system
     * @return array
     */
    public static function registerCheck($system)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/register/check?access_token='.$access_token;
        $data = self::curl($url,json_encode([]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['data']];
        }
    }
    /**
     * Desc:完成接入任务
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 09:20
     * @param $system
     * @param $access_info_item 6:完成spu接口，7:完成订单接口 / 19:完成二级商户号订单，8:完成物流接口，9:完成售后接口 / 20:完成二级商户号售后，10:测试完成，11:发版完成
     * @return array
     */
    public static function registerFinishAccessInfo($system,$access_info_item)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/register/finish_access_info?access_token='.$access_token;
        $data = self::curl($url,json_encode(['access_info_item'=>$access_info_item]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:场景接入申请
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 09:22
     * @param $system
     * @param $scene_group_id  1视频号
     * @return array
     */
    public static function registerApplyScene($system,$scene_group_id=1)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/register/apply_scene?access_token='.$access_token;
        $data = self::curl($url,json_encode(['scene_group_id'=>$scene_group_id]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }



    /**********************接入商品前必须接口*********************/
    /**
     * Desc:获取类目详情
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/13 09:48
     * @param $system
     * @return array
     */
    public static function catGet($system)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/cat/get?access_token='.$access_token;
        $data = self::curl($url,json_encode([]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            //返回所有三级类目  需要缓存 每天更新一次
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['third_cat_list']];
        }
    }
    /**
     * Desc:上传图片
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 09:28
     * @param $system
     * @param $resp_type  [响应格式0:media_id(目前只用于品牌申请品牌和类目)，1：返回链接, 2：返回微信支付media_id
     * @param $upload_type 0:图片流(resp_type=2需指定filename)，1:图片url
     * @param $img_url [upload_type=1时必传
     * @return array
     */
    public static function imgUpload($system,$resp_type,$upload_type,$img_url)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/img/upload?access_token='.$access_token;
        $params = [
            'resp_type'=>$resp_type,
            'upload_type'=>$upload_type,
            'img_url'=>$img_url
        ];
        $data = self::curl($url,$params,array('Content-Type: multipart/form-data'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['img_info']];
        }
    }
    /**
     * Desc:上传品牌信息
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 10:56
     * @param $system
     * @param $audit_req
     * @return array
     */
    public static function auditBrand($system,$audit_req)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/audit/audit_brand?access_token='.$access_token;
        $data = self::curl($url,json_encode(['audit_req'=>$audit_req]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['audit_id']];
        }
    }
    /**
     * Desc:上传类目资质
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 10:58
     * @param $system
     * @param $audit_req
     * @return array
     */
    public static function auditCategory($system,$audit_req)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/audit/audit_category?access_token='.$access_token;
        $data = self::curl($url,json_encode(['audit_req'=>$audit_req]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['audit_id']];
        }
    }
    /**
     * Desc:查询审核结果
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:02
     * @param $system
     * @param $audit_id
     * @return array
     */
    public static function auditResult($system,$audit_id)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/audit/result?access_token='.$access_token;
        $data = self::curl($url,json_encode(['audit_id'=>$audit_id]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['data']];
        }
    }



    /**********************商家入驻接口*********************/
    /**
     * Desc:获取类目列表
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:06
     * @param $system
     * @return array
     */
    public static function getCategoryList($system)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/account/get_category_list?access_token='.$access_token;
        $data = self::curl($url,json_encode([]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['data']];
        }
    }
    /**
     * Desc:获取品牌列表
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:07
     * @param $system
     * @return array
     */
    public static function getBrandList($system)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/account/get_brand_list?access_token='.$access_token;
        $data = self::curl($url,json_encode([]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['data']];
        }
    }
    /**
     * Desc:更新商家信息
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 09:07
     * @param $system
     * @param $params
     * @return array
     */
    public static function updateShopInfo($system,$params)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/account/update_info?access_token='.$access_token;
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:获取商家信息
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/28 15:45
     * @param $system
     * @return array
     */
    public static function getShopInfo($system)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/account/get_info?access_token='.$access_token;
        $data = self::curl($url,json_encode([]),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['data']];
        }
    }


    /**********************spu接口*********************/
    /**
     * Desc:添加商品
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 15:48
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuAdd($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/add?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id'],
            'title'=>$data['title'],
            'path'=>$data['path'],
            'desc_info'=>$data['desc_info'],
            'third_cat_id'=>$data['third_cat_id'],
            'brand_id'=>$data['brand_id'],
            'skus'=>$data['skus'],
            'scene_group_list'=>$data['scene_group_list'],
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:删除商品
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 15:48
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuDel($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/del?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id'],
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:撤回商品审核
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 15:50
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuDelAudio($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/del_audit?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id'],
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:获取商品
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 15:52
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuGet($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/get?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id'],
            'need_edit_spu'=>$data['need_edit_spu'],//默认0:获取线上数据, 1:获取草稿数据
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['spu'=>$data['spu'],'scene_group_list'=>$data['scene_group_list']]];
        }
    }
    /**
     * Desc:获取商品列表
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 15:58
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuGetList($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/get_list?access_token='.$access_token;
        $params = [
            'start_create_time'=>$data['start_create_time'],
            'end_create_time'=>$data['end_create_time'],
            'need_edit_spu'=>$data['need_edit_spu'],
            'page'=>$data['page']?$data['page']:1,
            'page_size'=>min($data['page_size'],100),
        ];
        if ($data['status']){
            $params['status'] = $data['status'];
        }
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['total_num'=>$data['total_num'],'spus'=>$data['spus'],'scene_group_list'=>$data['scene_group_list']]];
        }
    }
    /**
     * Desc:更新商品
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:02
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuUpdate($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/update?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id'],
            'title'=>$data['title'],
            'path'=>$data['path'],
            'head_img'=>$data['head_img'],
            'desc_info'=>$data['desc_info'],
            'third_cat_id'=>$data['third_cat_id'],
            'brand_id'=>$data['brand_id'],
            'skus'=>$data['skus'],
            'scene_group_list'=>$data['scene_group_list'],
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['data']];
        }
    }
    /**
     * Desc:上架商品
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:04
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuListing($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/listing?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:下架商品
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:05
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuDeListing($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/delisting?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }
    /**
     * Desc:免审核更新商品字段
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:08
     * @param $system
     * @param $data
     * @return array
     */
    public static function spuUpdateWithOutAudit($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/spu/update_without_audit?access_token='.$access_token;
        $params = [
            'out_product_id'=>$data['out_product_id'],
            'skus'=>$data['skus'],
        ];
        if ($data['limited_discount_info']){
            $params['limited_discount_info'] = $data['limited_discount_info'];
        }
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }


    /**********************订单接口*********************/
    //缺失生成订单并获取titcket 接口
    //缺失同步订单支付结果 接口
    /**
     * Desc:获取订单
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:19
     * @param $system
     * @param $data
     * @return array
     */
    public static function OrderGet($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/order/get?access_token='.$access_token;
        $params = [
            'order_id'=>$data['order_id'],
            'out_order_id'=>$data['out_order_id'],
            'openid'=>$data['openid']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data];
        }
    }
    /**
     * Desc:按照推广员获取订单
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:21
     * @param $system
     * @param $data
     * @return array
     */
    public static  function orderGetListByFinder($system,$data){
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/order/get_list_by_finder?access_token='.$access_token;
        $params = [
            'start_pay_time'=> isset($data['start_pay_time'])?$data['start_pay_time']:'',
            'end_pay_time'=> isset($data['end_pay_time'])?$data['end_pay_time']:'',
            'promoter_id'=>$data['promoter_id'],           //推广员ID
            'promoter_openid'=>$data['promoter_openid'],   //推广员openid
            'page'=> $data['page']?$data['page']:1,
            'page_size' => $data['page_size']?$data['page_size']:10
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data];
        }

    }
    /**
     * Desc:按照分享员获取订单
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:22
     * @param $system
     * @param $data
     * @return array
     */
    public static function orderGetListBySharer($system,$data){
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/order/get_list_by_sharer?access_token='.$access_token;

        $params = [
            'start_pay_time'=> isset($data['start_pay_time'])?$data['start_pay_time']:'',
            'end_pay_time'=> isset($data['end_pay_time'])?$data['end_pay_time']:'',
            'sharer_openid'=>$data['sharer_openid'],   //分享员openid
            'page'=> $data['page']?$data['page']:1,
            'page_size' => $data['page_size']?$data['page_size']:10
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data];
        }
    }
    /**
     * Desc:获取订单列表
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:23
     * @param $system
     * @param $data
     * @return array
     */
    public static function orderGetList($system,$data){
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/order/get_list?access_token='.$access_token;

        $params = [
            'start_create_time'=> isset($data['start_create_time'])?$data['start_create_time']:'',
            'end_pay_time'=> isset($data['end_create_time'])?$data['end_create_time']:'',
            'sharer_openid'=>$data['sort_order'],  //排序 1:desc, 2:asc
            'page'=> $data['page']?$data['page']:1,
            'page_size' => $data['page_size']?$data['page_size']:10
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data];
        }
    }


    /**********************物流接口*********************/
    //缺失订单发货 接口
    //缺失订单确认收货 接口


    /**********************售后接口*********************/
    //缺失创建售后 接口
    /**
     * Desc:获取售后
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:18
     * @param $system
     * @param $data
     * @return array
     */
    public static function afterSaleGet($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/aftersale/get?access_token='.$access_token;
        $params = [
            'out_order_id'=>$data['out_order_id'],
            'openid'=>$data['openid']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>$data['aftersale_infos']];
        }
    }
    /**
     * Desc:更新售后
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:22
     * @param $system
     * @param $data
     * @return array
     */
    public static function afterSaleUpdate($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/aftersale/update?access_token='.$access_token;
        $params = [
            'out_order_id'=>$data['out_order_id'],
            'out_aftersale_id'=>$data['out_aftersale_id'],
            'openid'=>$data['openid'],
            'status'=>$data['status'],//0:未受理,1:用户取消,2:商家受理中,3:商家逾期未处理,4:商家拒绝退款,5:商家拒绝退货退款,6:待买家退货,7:退货退款关闭,8:待商家收货,11:商家退款中,12:商家逾期未退款,13:退款完成,14:退货退款完成,15:换货完成,16:待商家发货,17:待用户确认收货,18:商家拒绝换货,19:商家已收到货
            'finish_all_aftersale'=>$data['finish_all_aftersale'],
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }


    /**********************优惠券接口*********************/
    //缺失优惠券全部  接口


    /**********************推广员接口*********************/
    //缺失推广员  接口

    /**********************分享员接口*********************/
    /**
     * Desc:绑定分享员接口
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:31
     * @param $system
     * @param $data
     * @return array
     */
    public static function sharerBind($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/sharer/bind?access_token='.$access_token;
        $params = [
            'openids'=>$data['openids']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['result_list'=>$data['result_list']]];
        }
    }
    /**
     * Desc:解绑分享员
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:34
     * @param $system
     * @param $data
     * @return array
     */
    public static function sharerUnbind($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/sharer/unbind?access_token='.$access_token;
        $params = [
            'openids'=>$data['openids']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['success_list'=>$data['success_list']]];
        }
    }
    /**
     * Desc:获取已经绑定的分享员的列表
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:36
     * @param $system
     * @param $data
     * @return array
     */
    public static function sharerGetSharerList($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/sharer/get_sharer_list?access_token='.$access_token;
        $params = [
            'page'=>$data['page']?$data['page']:1,
            'page_size'=>min($data['page_size'],100),
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['sharers'=>$data['sharers'],'total_num'=>$data['total_num']]];
        }
    }
    /**
     * Desc:获取分享员
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:41
     * @param $system
     * @param $data
     * @return array
     */
    public static function sharerSearchSharer($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/sharer/search_sharer?access_token='.$access_token;
        $params = [
            'openid'=>$data['openid']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['invited_time'=>$data['invited_time'],'bind_time'=>$data['bind_time'],'nickname'=>$data['nickname'],'bind_status'=>$data['bind_status']]];
        }
    }
    /**
     * Desc:获取分享员的总带货数据
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:43
     * @param $system
     * @param $data
     * @return array
     */
    public static function sharerGetSharerDataSummary($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/sharer/get_sharer_data_summary?access_token='.$access_token;
        $params = [
            'openid'=>$data['openid']
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['gmv'=>$data['gmv'],'order_cnt'=>$data['order_cnt'],'user_cnt'=>$data['user_cnt']]];
        }
    }
    /**
     * Desc:获取分享员的直播间订单汇总
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:46
     * @param $system
     * @param $data
     * @return array
     */
    public static function sharerGetSharerLiveOrderList($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/sharer/get_sharer_live_order_list?access_token='.$access_token;
        $params = [
            'openid'=>$data['openid'],
            'live_export_id'=>$data['live_export_id'],
            'page_size'=>min($data['page_size'],100),
            'page'=>$data['page']?$data['page']:1,
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['total_num'=>$data['total_num'],'orders'=>$data['orders']]];
        }
    }
    /**
     * Desc:获取分享员的直播间带货数据汇总
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/11 16:49
     * @param $system
     * @param $data
     * @return array
     */
    public static function sharerGetSharerLiveSummaryList($system,$data)
    {
        $access_token = self::getAccessToken($system);
        $url = 'https://api.weixin.qq.com/shop/sharer/get_sharer_live_summary_list?access_token='.$access_token;
        $params = [
            'openid'=>$data['openid'],
            'page_size'=>min($data['page_size'],100),
            'page'=>$data['page']?$data['page']:1,
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>['total_num'=>$data['total_num'],'lives'=>$data['lives']]];
        }
    }
    //缺失视频号直播分享归因 接口


    /**
     * Desc:数据表安装方法
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:31
     */
    public static function installSql()
    {
        $sql = "

            CREATE TABLE IF NOT EXISTS `yx_component_task_item` (
            `id` int(11) not null AUTO_INCREMENT,
            `appid` varchar(32) not null default '' comment '绑定的小程序appid',
            `spu_audit_success` int(2) not null default -1 comment '上传商品并审核成功，0:未成功，1:成功',
            `ec_order_success` int(2) not null default -1 comment '发起一笔订单并支付成功，0:未成功，1:成功',
            `send_delivery_success` int(2) not null default -1 comment '物流接口调用成功，0:未成功，1:成功',
            `ec_after_sale_success` int(2) not null default -1 comment '售后接口调用成功，0:未成功，1:成功',
            `spu_audit_finished` int(2) not null default -1 comment '商品接口调试完成，0:未完成，1:已完成',
            `ec_order_finished` int(2) not null default -1 comment '订单接口调试完成，0:未完成，1:已完成',
            `send_delivery_finished` int(2) not null default -1 comment '物流接口调试完成，0:未完成，1:已完成',
            `ec_after_sale_finished` int(2) not null default -1 comment '售后接口调试完成，0:未完成，1:已完成',
            `test_api_finished` int(2) not null default -1 comment '测试完成，0:未完成，1:已完成',
            `deploy_wxa_finished` int(2) not null default -1 comment '发版完成，0:未完成，1:已完成',
            `open_product_task_finished` int(2) not null default -1 comment '完成自定义组件全部任务 0:未完成 1:已完成',
            `scene_group_list` json comment '场景接入相关',
            `created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
            `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
            PRIMARY KEY (`id`)
            )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='任务实例-视频号小程序自定义交易组件';

            CREATE TABLE IF NOT EXISTS `yx_component_audit_item` (
                `id` int(11) not null AUTO_INCREMENT,
                `appid` varchar(32) not null default '' comment '绑定的小程序appid',
                `audit_id` varchar(32) not null default '' comment '审核单id',
                `license` json  comment '相关证明材料图片 数组',
                `info` json comment '相关信息',
                `scene_group_list` json comment '场景',
                `type` varchar(16) not null default 'brand' comment '审核单类型 brand 品牌 category类目',
                `status` tinyint(1)  not null default 0 comment '审核状态 0审核中1审核成功9审核失败',
                `brand_id` int(11) not null default 0 comment '品牌审核特有字段 品牌id',
                `reject_reason` varchar(32) not null default '' comment '审核说明',
                `created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
                `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
            PRIMARY KEY (`id`)
            )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='品牌、类目审核单-视频号小程序自定义交易组件';

            
            
            CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_component_goods` (
            `id` int(11) not null AUTO_INCREMENT,
            `out_product_id` varchar(32) not null default '' comment '商家自定义商品ID',
            `title` varchar(60) not null default '' comment '标题',
            `path` varchar(128) not null default '' comment '绑定小程序商品路径',
            `direct_path` varchar(128) not null default '' comment '商品立即购买链接',
            `head_img` text comment '主图，多张，列表，最多9张',
            `desc_info` text comment '商品详情，图文',
            `brand_id` int(11)  not null default 0 comment '品牌ID',
            `third_cat_id` int(11)  not null default 0 comment '第三级类目ID',
            `third_cat_name` varchar(50) not null  default '' comment '第三级类目ID',
            `info_version` text comment '预留字段，用于版本控制',
            `sku` text comment 'sku数组',
            `scene_group_list` text comment '商品使用场景',
            `status` tinyint(1)  not null default 1 comment '线上状态 1未审核2审核中3审核失败4审核成功',
            `edit_status` tinyint(1)  not null default 0 comment '草稿状态 0初始值5上架11自主下架13违规风控下架',
            `created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
            `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
            `themeid` varchar(11) not null default '' comment '活动id',
            PRIMARY KEY (`id`)
            )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='自定义组件同步商品表';
            
            CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_component_order_refund` (
            `id` int(11) not null AUTO_INCREMENT,
            `out_order_id` varchar(32) not null default '' comment '商家自定义订单id',
            `out_aftersale_id` varchar(32) not null default '' comment '商家自定义售后id',
            `refund_amount` decimal(6,2) not null default 0.00 comment '退款金额 元',
            `refund_reason` text comment '退款原因',
            `openid` varchar(32) not null default '' comment '用户的openid',
            `type` tinyint(1)  not null default 1 comment '售后类型，1:退款,2:退款退货,3:换货',
            `status` tinyint(1)  not null default 0 comment '0:未受理,1:用户取消,2:商家受理中,3:商家逾期未处理,4:商家拒绝退款,5:商家拒绝退货退款,6:待买aftersale_infos[].家退货,7:退货退款关闭,8:待商家收货,11:商家退款中,12:商家逾期未退款,13:退款完成,14:退货退款完成',
            `apply_time` timestamp default '0000-00-00 00:00:00' comment '申请售后时间',
            `created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
            `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
            `themeid` varchar(11) not null default '' comment '活动id',
            PRIMARY KEY (`id`)
            )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='自定义组件订单售后表';
        ";
    }

    /**
     * Desc:获取access_token
     * Author:cbw
     * Email:17854288794@163.com
     * Time: 2023/7/7 11:31
     * @param $system
     * @return mixed
     */
    public static function getAccessToken($system){
        return getAccessToken($system);
    }
}